package com.sky.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sky.constant.MessageConstant;
import com.sky.exception.BaseException;
import com.sky.exception.OrderBusinessException;
import com.sky.properties.BaiduMapsProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Description BaiduMapsUtil
 * @Author zhuzi
 * @Date 2023/10/07
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class BaiduMapsUtil {

    // 地理编码接口
    private final static String GEOCODING = "https://api.map.baidu.com/geocoding/v3";

    // 骑行路线规划接口
    private final static String DIRECTION = "https://api.map.baidu.com/direction/v2/riding";

    private final BaiduMapsProperties baiduMapsProperties;

    /**
     * 获取地理编码
     *
     * @param address String
     * @return String
     */
    public String geocoding(String address) {
        // 准备请求参数
        Map<String, String> map = new HashMap<>();
        map.put("address", address); // 设置地址
        map.put("ak", baiduMapsProperties.getAk()); // 设置ak
        map.put("output", "json"); // 设置输出格式为json

        // 调用接口获取json格式的响应体
        String body = HttpClientUtil.doGet(GEOCODING, map);

        // 解析body，判断是否有效
        JSONObject jsonObject = JSON.parseObject(body);
        if (!"0".equals(jsonObject.getString("status"))) {
            // 本次API访问状态，如果成功返回0，如果失败返回其他数字
            return null;
        }

        // 获取经纬度
        JSONObject location = jsonObject.getJSONObject("result").getJSONObject("location");
        String lat = location.getString("lat");
        String lng = location.getString("lng");
        // return lat.substring(0, lat.lastIndexOf(".") + 7) + "," + lng.substring(0, lng.lastIndexOf(".") + 7);
        return lat + "," + lng;
    }

    public Integer direction(String origin, String destination) {
        // 准备请求参数
        Map<String, String> map = new HashMap<>();
        map.put("origin", origin);
        map.put("destination", destination);
        map.put("ak", baiduMapsProperties.getAk());

        // 调用接口获取json格式的响应体
        String body = HttpClientUtil.doGet(DIRECTION, map);

        // 解析body，判断是否有效
        JSONObject jsonObject = JSON.parseObject(body);
        log.info("Get jsonObject: {}", jsonObject);
        switch (jsonObject.getString("status")) {
            /*
            0-成功
            1-服务内部错误
            2-参数无效
            2002-无骑行路线
             */
            case "0":
                JSONArray routes = (JSONArray) jsonObject.getJSONObject("result").get("routes");
                // 这里可以优化：比较各个路线，选择最优方案，这里是直接使用了第一个路线方案
                Integer distance = (Integer) ((JSONObject) routes.get(0)).get("distance");
                log.info("Get distance: {}", distance);
                return distance;
            case "1":
                return -1;
            case "2":
                return -2;
            default:
                return null;
        }
    }

}
